# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

source("misc.R")
source("metrics.R")

basicConfig()

#fa <- read.graph(file = "fa-core-graphml.xml", format = "graphml")
fa <- erdos.renyi.game(4843, 61544, type = "gnm", directed = TRUE)
V(fa)$id <- paste(c(1:length(V(fa))), sep = "")
E(fa)$id <- paste(c(1:length(E(fa))), sep = "")
fa <- get.main.cc(fa)

fa <- simplify(fa)

#dd <- read.graph(file = "dd-core-graphml.xml", format = "graphml")
dd <- erdos.renyi.game(1496, 4766, type = "gnm", directed = TRUE)
V(dd)$id <- paste(c(1:length(V(dd))), sep = "")
E(dd)$id <- paste(c(1:length(E(dd))), sep = "")
dd <- get.main.cc(dd)

dd <- simplify(dd)

dfa <- mean(degree(fa)) 
ddd <- mean(degree(dd))

print(paste("Avg FA degree: ", dfa))
print(paste("Avg DD degree: ", ddd))

print(paste("Network entropy FA: ", get.network.entropy(get.adjacency(fa), normalize = TRUE)))
print(paste("Network entropy DD: ", get.network.entropy(get.adjacency(dd), normalize = TRUE)))

print(paste("Degree entropy FA: ", get.degree.entropy(fa, normalize = TRUE)))
print(paste("Degree entropy DD: ", get.degree.entropy(dd, normalize = TRUE)))

print(paste("FA CPL: ", get.graph.stats(fa, c("cpl")["cpl"])))
print(paste("DD CPL: ", get.graph.stats(dd, c("cpl")["cpl"])))

order <- 1
spfa <- shortest.paths(fa, V(fa))
spdd <- shortest.paths(dd, V(dd))

vafar <- c()
vaddr <- c()

# get neighborhood of each vertex
for (v in V(fa))
{
	lfa <- V(fa)[v]$name
	
	if (lfa %in% V(dd)$name)
	{
		vidsfa <- which(spfa[v + 1,] <= order) - 1
		vdd <- which(V(dd)$name == lfa) - 1
		vidsdd <- which(spdd[vdd + 1,] <= order) - 1
	
		#vids <- neighborhood(fa, order, v)[[1]]
	
		#labelsfa <- V(fa)[vidsfa]$name
		#ldd <- V(dd)[v]$name
		#labelsdd <- V(dd)[vidsdd]$name
	
		subfa <- subgraph(fa, vidsfa)
		subdd <- subgraph(dd, vidsdd)

		#vafar[lfa] <- get.robustness(subfa)$r
		#vaddr[lfa] <- get.robustness(subdd)$r
		
		#vafar[lfa] <- get.graph.stats(subfa, c("gtrans"))["gtrans"]
		#ddv <- get.graph.stats(subdd, c("gtrans"))["gtrans"]
		#vaddr[lfa] <- ifelse(!is.nan(ddv), ddv, 0)
		
		#vafar[lfa] <- get.graph.stats(subfa, c("eff"))["eff"]
		#vaddr[lfa] <- get.graph.stats(subdd, c("eff"))["eff"]
		
		#vafar[lfa] <- get.graph.stats(subfa, c("cpl"))["cpl"]
		#vaddr[lfa] <- get.graph.stats(subdd, c("cpl"))["cpl"]
		
		#vafar[lfa] <- get.graph.stats(subfa, c("msccs"))["msccs"] / length(V(subfa))
		#vaddr[lfa] <- get.graph.stats(subdd, c("msccs"))["msccs"] / length(V(subdd))
		
		vafar[lfa] <- get.degree.entropy(subfa, normalize = TRUE)
		vaddr[lfa] <- get.degree.entropy(subdd, normalize = TRUE)
		
		#values[lfa] <- (length(vidsfa)/dfa) / (length(vidsdd)/ddd)
	}

	#png(paste(l, ".png", sep = ""), width = 800, height = 800)
	#V(sub)[V(sub)$name == l]$color <- "red"
	#plot(sub, layout=layout.fruchterman.reingold, vertex.label = labels, vertex.size = 18)
	#dev.off()
}


print(paste("FA: ", mean(vafar)))
print(paste("DD: ", mean(vaddr)))
